﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Niknak.Services.TaskScheduler.UI
{
    public static class Utils
    {
        /// <summary>
        /// See http://stackoverflow.com/questions/3261451/using-a-bitmask-in-c-sharp
        /// </summary>
        public static class FlagsHelper
        {
            public static void Set<T>(ref T flags, T flag) where T : struct
            {
                int flagsValue = (int)(object)flags;
                int flagValue = (int)(object)flag;

                flags = (T)(object)(flagsValue | flagValue);
            }

            public static void Unset<T>(ref T flags, T flag) where T : struct
            {
                int flagsValue = (int)(object)flags;
                int flagValue = (int)(object)flag;

                flags = (T)(object)(flagsValue & (~flagValue));
            }
        }

        public static string GetEnumDescription(Enum value)
        {
            DescriptionAttribute[] attributes = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
            return (((attributes != null) && (attributes.Length > 0)) ? attributes[0].Description : value.ToString());
        }

        public static int CalculateNumberOfPages(int totalNumberOfItems, int pageSize)
        {
            var result = totalNumberOfItems % pageSize;
            if (result == 0)
                return totalNumberOfItems / pageSize;
            else
                return totalNumberOfItems / pageSize + 1;
        }
    }
}
